//
//  PartEvaluateView.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/6/20.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "PartEvaluateView.h"

@interface PartEvaluateView()<UITableViewDelegate,UITableViewDataSource>

proAssign BOOL isGestureEnd;//手势是否已经结束

@end

@implementation PartEvaluateView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gestureStateBegan:) name:@"GestureRecognizerStateBegan" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gestureStateEnd:) name:@"GestureRecognizerStateEnded" object:nil];
        
        [self addViews];
    }
    return self;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)addViews {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.tableFooterView = [UIView new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self addSubview:self.tableView];
}

#pragma mark ---通知方法
- (void)gestureStateBegan:(NSNotification *)not {
    
    BOOL isMore = _tableView.contentOffset.y >= (_tableView.contentSize.height - _tableView.height);
    if (isMore) {
        self.isGestureEnd = NO;
    }
}

- (void)gestureStateEnd:(NSNotification *)not {
    //    手势已经结束
    BOOL isMore = _tableView.contentOffset.y > (_tableView.contentSize.height - _tableView.height);
    if (isMore) {
        //如果滑动的偏移量超出最大的内容范围
        CGFloat between = _tableView.contentOffset.y - (_tableView.contentSize.height - _tableView.height);
        if (between >= 70) {
            self.isGestureEnd = YES;
        }
    }
}

#pragma mark -
#pragma mtak tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 20;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    cell.textLabel.text = [NSString stringWithFormat:@"评论%d",(int)indexPath.row];
    if (indexPath.row%2==0) {
        cell.backgroundColor = [[UIColor systemPinkColor]colorWithAlphaComponent:0.5];
        cell.textLabel.textColor = [UIColor whiteColor];
    } else {
        cell.backgroundColor = [UIColor whiteColor];
        cell.textLabel.textColor = [UIColor darkGrayColor];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 55;
}

@end
